# 短视频解析vew
import logging
from urllib.parse import unquote

from rest_framework.decorators import action, api_view
from rest_framework.views import APIView

from app.service.shortvideo_service import DouYinParser, KuaiShouParser, PipixiaParser
from app.util import mhttp

douyin = DouYinParser()
kuaishou = KuaiShouParser()
pipiXia = PipixiaParser()
logger = logging.getLogger(__name__)

def parse(request):
    if request.method == 'GET':
        copyUrl = request.GET.get('copyUrl')
        if copyUrl is None:
            return mhttp.params_error('参数错误')
        copyUrl = unquote(copyUrl)
        # 提取http
        s = copyUrl.find('http')
        e = copyUrl.find(' ', s + len('http'))
        if e<0:
            copyUrl = copyUrl[s:]
        else:
            copyUrl = copyUrl[s:e]
        dict = {}
        logger.info('come one shortvideo url: %s'%copyUrl)
        if copyUrl.find('v.kuaishouapp.com') > 0:
            dict = kuaishou.getVideoInfo(copyUrl)
        elif copyUrl.find('v.douyin.com') > 0:
            dict = douyin.noMark(copyUrl)
        elif copyUrl.find('pipix.com') > 0:
            dict = pipiXia.noMark(copyUrl)
        logger.info('parse result: %s'%dict)
        return mhttp.result(data=dict)
